package com.spingweb.controller;

import com.spingweb.util.CommonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 在spring中不启动server和browser测试。
 */
@Controller
@RequestMapping("/2019")
public class For2019Controller {

    @RequestMapping("home")
    public String home() {
        // 返回的视图和请求视图一样。
        return "2019/home";

    }

    @PostMapping(value = "upload")
    @ResponseBody
    public CommonResult upload(@RequestParam("f1") MultipartFile file) {
        String name = file.getOriginalFilename(); //去掉文件的路径，换成工程下面的文件。
        // 这个路径还是要注意
        String path = "";
        try {
            File temp = new File(path);
            File file1 = new File(temp.getCanonicalPath() + "/" + name);
            file.transferTo(file1);
            System.out.println(file1.getCanonicalPath());
        } catch (IOException e) {
            return CommonResult.failure("上传文件失败");
        }
        return CommonResult.success("上传文件成功");
    }


    @GetMapping("upload")
    // 用户先发出一个请求
    public String upload() {
        return "2019/uploadFile";
    }
}
